import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { defRangeShortcuts, formatToDate } from '@/utils/dateUtil';
import { validate } from '@/utils/validateUtil';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // 管理员ID
  public deptId = 0; // 部门ID
  public roleId = 10; // 角色ID
  public realName = ''; // 真实姓名
  public username = ''; // 帐号
  public passwordHash = ''; // 密码
  public salt = ''; // 密码盐
  public passwordResetToken = ''; // 密码重置令牌
  public integral = 0; // 积分
  public balance = 0; // 余额
  public avatar = ''; // 头像
  public sex = 1; // 性别
  public qq = ''; // qq
  public email = ''; // 邮箱
  public mobile = ''; // 手机号码
  public birthday = ''; // 生日
  public cityId = null; // 城市编码
  public address = ''; // 联系地址
  public pid = 0; // 上级管理员ID
  public level = 1; // 关系树等级
  public tree = ''; // 关系树
  public inviteCode = ''; // 邀请码
  public cash = null; // 提现配置
  public lastActiveAt = ''; // 最后活跃时间
  public remark = ''; // 备注
  public status = 1; // 状态
  public createdAt = ''; // 创建时间
  public updatedAt = ''; // 修改时间

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  username: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入帐号',
  },
  passwordHash: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入密码',
  },
  salt: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入密码盐',
  },
  qq: {
    required: false,
    trigger: ['blur', 'input'],
    type: 'string',
    validator: validate.qq,
  },
  email: {
    required: false,
    trigger: ['blur', 'input'],
    type: 'string',
    validator: validate.email,
  },
  mobile: {
    required: false,
    trigger: ['blur', 'input'],
    type: 'string',
    validator: validate.phone,
  },
  pid: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'number',
    message: '请输入上级管理员ID',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'id',
    component: 'NInputNumber',
    label: '管理员ID',
    componentProps: {
      placeholder: '请输入管理员ID',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NSelect',
    label: '状态',
    defaultValue: null,
    componentProps: {
      placeholder: '请选择状态',
      options: dict.getOption('sys_normal_disable'),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'createdAt',
    component: 'NDatePicker',
    label: '创建时间',
    componentProps: {
      type: 'datetimerange',
      clearable: true,
      shortcuts: defRangeShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: '管理员ID',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '部门ID',
    key: 'deptId',
    align: 'left',
    width: -1,
  },
  {
    title: '角色ID',
    key: 'roleId',
    align: 'left',
    width: -1,
  },
  {
    title: '真实姓名',
    key: 'realName',
    align: 'left',
    width: -1,
  },
  {
    title: '帐号',
    key: 'username',
    align: 'left',
    width: -1,
  },
  {
    title: '密码',
    key: 'passwordHash',
    align: 'left',
    width: -1,
  },
  {
    title: '密码盐',
    key: 'salt',
    align: 'left',
    width: -1,
  },
  {
    title: '密码重置令牌',
    key: 'passwordResetToken',
    align: 'left',
    width: -1,
  },
  {
    title: '积分',
    key: 'integral',
    align: 'left',
    width: -1,
  },
  {
    title: '余额',
    key: 'balance',
    align: 'left',
    width: -1,
  },
  {
    title: '头像',
    key: 'avatar',
    align: 'left',
    width: -1,
  },
  {
    title: '性别',
    key: 'sex',
    align: 'left',
    width: -1,
  },
  {
    title: 'qq',
    key: 'qq',
    align: 'left',
    width: -1,
  },
  {
    title: '邮箱',
    key: 'email',
    align: 'left',
    width: -1,
  },
  {
    title: '手机号码',
    key: 'mobile',
    align: 'left',
    width: -1,
  },
  {
    title: '生日',
    key: 'birthday',
    align: 'left',
    width: -1,
    render(row: State) {
      return formatToDate(row.birthday);
    },
  },
  {
    title: '城市编码',
    key: 'cityId',
    align: 'left',
    width: -1,
  },
  {
    title: '联系地址',
    key: 'address',
    align: 'left',
    width: -1,
  },
  {
    title: '上级管理员ID',
    key: 'pid',
    align: 'left',
    width: -1,
  },
  {
    title: '邀请码',
    key: 'inviteCode',
    align: 'left',
    width: -1,
  },
  {
    title: '最后活跃时间',
    key: 'lastActiveAt',
    align: 'left',
    width: -1,
  },
  {
    title: '备注',
    key: 'remark',
    align: 'left',
    width: -1,
  },
  {
    title: '状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('sys_normal_disable', row.status);
    },
  },
  {
    title: '创建时间',
    key: 'createdAt',
    align: 'left',
    width: -1,
  },
  {
    title: '修改时间',
    key: 'updatedAt',
    align: 'left',
    width: -1,
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['sys_normal_disable']);
}